<template>
  <div class="back-html">
    <!-- 装饰性背景元素 -->
    <div class="decoration-bg">
      <div class="floating-line line-1"></div>
      <div class="floating-line line-2"></div>
    </div>

    <el-row class="home-container">
      <!-- 标签内容部分 -->
      <el-col :lg="{span: 18, offset: 0}" :span="24">
        <div class="tag-content">
          <el-card class="tag-card">
            <!-- 标签标题和描述 -->
            <div class="tag-header">
              <h2 class="tag-title">
                <i class="icon-tag-fill"></i>
                文章标签
              </h2>
              <p class="tag-description">标签云展示，发现更多相关主题和内容标记</p>
              <div class="tag-stats">共有 {{ tags.length }} 个标签</div>
            </div>

            <!-- 标签云展示 -->
            <div class="tag-cloud-modern">
              <div
                v-for="(item, index) in tags"
                :key="item.id"
                class="tag-item"
                @click="selectTag(item)"
                :class="[
                  `tag-size-${getTagSize(item.articleCount)}`,
                  { active: selectedTag && selectedTag.id === item.id }
                ]"
              >
                <div class="tag-content-wrapper">
                  <span class="tag-name">{{ item.name }}</span>
                  <span class="tag-count">{{ item.articleCount }}</span>
                </div>
                <div class="tag-hover-effect"></div>
              </div>
            </div>

            <!-- 选中标签的文章列表 -->
            <div class="selected-tag-articles" v-if="selectedTag && articles.length > 0">
              <div class="selected-tag-header">
                <h3 class="selected-tag-title">
                  <i class="icon-document"></i>
                  标签「{{ selectedTag.name }}」下的文章
                </h3>
                <div class="selected-tag-info">
                  共找到 {{ totalSize }} 篇文章
                </div>
              </div>

              <div class="articles-list">
                <div
                  v-for="article in articles"
                  :key="article.id"
                  class="article-item"
                  @click="navigateToArticle(article.id)"
                >
                  <!-- 文章序号和类型图标 -->
                  <div class="article-index">
                    <div class="index-number">{{ articles.indexOf(article) + 1 + (currentPage - 1) * pageSize }}</div>
                    <div class="article-type-icon">
                      <i class="icon-document-text"></i>
                    </div>
                  </div>

                  <div class="article-cover" v-if="article.coverImage">
                    <el-image :src="article.coverImage" fit="cover"></el-image>
                    <!-- 封面装饰 -->
                    <div class="cover-overlay">
                      <i class="icon-image"></i>
                    </div>
                  </div>

                  <!-- 无封面时的占位图标 -->
                  <div class="article-cover-placeholder" v-else>
                    <i class="icon-document-text"></i>
                  </div>

                  <div class="article-info">
                    <div class="article-header">
                      <h4 class="article-title">
                        <i class="icon-article"></i>
                        {{ article.title }}
                      </h4>
                      <!-- 置顶标识 -->
                      <div class="article-badges">
                        <span v-if="article.isWeight === 1" class="badge-top">
                          <i class="icon-pin"></i>
                          置顶
                        </span>
                      </div>
                    </div>

                    <p class="article-summary" v-if="article.summary">
                      <i class="icon-quote"></i>
                      {{ article.summary }}
                    </p>

                    <div class="article-meta">
                      <span class="meta-item">
                        <el-icon color="#4ecdc4"><Calendar/></el-icon>
                        {{ formatDate(article.createTime) }}
                      </span>
                      <span class="meta-item" v-if="article.viewCounts">
                        <el-icon color="#ff6b6b"><View/></el-icon>
                        <span v-if="article.viewCounts >= 1000 && article.viewCounts < 10000">{{ Math.floor(article.viewCounts / 1000) }}k</span>
                        <span v-else-if="article.viewCounts >= 10000">{{ Math.floor(article.viewCounts / 10000) }}w</span>
                        <span v-else>{{ article.viewCounts }}</span>
                      </span>
                      <span class="meta-item" v-if="article.categoryName">
                        <el-icon color="#ff69b4"><Folder/></el-icon>
                        {{ article.categoryName }}
                      </span>
                      <span class="meta-item" v-if="article.readTime">
                        <el-icon color="#f39c12"><Timer/></el-icon>
                        {{ article.readTime }}分钟
                      </span>
                    </div>
                  </div>

                  <!-- 右侧箭头图标 -->
                  <div class="article-arrow">
                    <i class="icon-arrow-right-circle"></i>
                  </div>
                </div>
              </div>

              <!-- 分页 -->
              <el-pagination
                v-if="totalSize > pageSize"
                v-model:currentPage="currentPage"
                :page-size="pageSize"
                layout="prev, pager, next"
                :total="totalSize"
                @current-change="handleCurrentChange"
                class="article-pagination"
              />
            </div>
          </el-card>
        </div>
      </el-col>

      <!-- 右侧边栏 -->
      <el-col :lg="{span: 6, offset: 0}" :span="0" v-if="blogInfo">
        <div class="right-sidebar">
          <!-- Profile Card -->
          <ProfileCard :blogInfo="blogInfo"/>

          <!-- Announcement Card -->
          <AnnouncementCard :blogInfo="blogInfo"/>

          <!-- Recommended Articles -->
          <ArticleCard :articleList="recentArticles"/>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import request from 'utils/request.js'
import {markRaw, nextTick, onMounted, ref, computed} from "vue";
import ProfileCard from "../../components/ProfileCard.vue";
import ArticleCard from "../../components/RecommendedArticles.vue";
import AnnouncementCard from "../../components/AnnouncementCard.vue";
import {useRouter} from 'vue-router';
import {Calendar, Folder, View, Timer} from '@element-plus/icons-vue';

interface Article {
  id: number;
  title: string;
  summary: string;
  coverImage: string;
  createTime: string;
  viewCounts: number;
  isWeight: number;
  categoryId: number;
  categoryName: string;
  tags: string[];
  readTime: number;
}

interface BlogInfo {
  webSite: {
    title?: string;
  };
}

interface Tag {
  id: number;
  name: string;
  articleCount: number;
}

const router = useRouter();
const tags = ref<Tag[]>([]);
const blogInfo = ref<BlogInfo>({
  webSite: {}
});
const recentArticles = ref<Article[]>([]);

// 选中的标签和相关文章
const selectedTag = ref<Tag | null>(null);
const articles = ref<Article[]>([]);
const currentPage = ref(1);
const pageSize = ref(10);
const totalSize = ref(0);

// 根据文章数量确定标签大小
const getTagSize = (count: number) => {
  if (count >= 10) return 'xl';
  if (count >= 7) return 'lg';
  if (count >= 5) return 'md';
  if (count >= 3) return 'sm';
  return 'xs';
};

// 选择标签
const selectTag = (tag: Tag) => {
  selectedTag.value = tag;
  currentPage.value = 1;
  getArticlesByTag();
};

// 获取指定标签的文章
const getArticlesByTag = () => {
  if (!selectedTag.value) return;

  request.get('/blog/getArticleByTagId', {
    params: {
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      tagId: selectedTag.value.id
    }
  }).then((res: any) => {
    nextTick(() => {
      articles.value = markRaw(res.data.records || []);
      totalSize.value = res.data.total || 0;
    });
  }).catch((error) => {
    console.error('获取标签文章失败:', error);
    articles.value = [];
    totalSize.value = 0;
  });
};

// 导航到文章详情
const navigateToArticle = (articleId: number) => {
  router.push(`/articles/${articleId}`);
};

// 格式化日期
const formatDate = (dateString: string) => {
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};

// 分页处理
const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  getArticlesByTag();
};

const getTags = () => {
  request.get("/tag/getTags").then((res: any) => {
    nextTick(() => {
      tags.value = markRaw(res.data);
      // 默认选中第一条标签
      if (tags.value.length > 0) {
        selectTag(tags.value[0]);
      }
    })
  })
}

const getBlogInfo = () => {
  request.get("/index/getBlogInfo").then((res: any) => {
    nextTick(() => {
      blogInfo.value = markRaw(res.data);
    })
  })
}

const getRecentArticles = () => {
  request.get("/blog/getArticles", {
    params: {
      pageNum: 1,
      pageSize: 5,
      title: '',
      categoryId: null
    }
  }).then((res: any) => {
    nextTick(() => {
      recentArticles.value = markRaw(res.data.records);
    })
  })
}

onMounted(() => {
  getTags();
  getBlogInfo();
  getRecentArticles();
})
</script>

<style scoped lang="scss">
@import '@/assets/styles/variables.scss';

.back-html {
  background: var(--gradient-page);
  min-height: 100vh;
  padding-top: 10px;
  margin: 0 10px 10px 10px;
  position: relative;
  overflow: hidden;
}

// 装饰性背景元素
.decoration-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.floating-line {
  position: absolute;
  background: var(--bg-decoration);
  animation: lineFloat 8s ease-in-out infinite;
}

.line-1 {
  width: 200px;
  height: 2px;
  top: 30%;
  right: 5%;
  transform: rotate(45deg);
  animation-delay: 1s;
}

.line-2 {
  width: 150px;
  height: 2px;
  bottom: 40%;
  left: 8%;
  transform: rotate(-30deg);
  animation-delay: 3s;
}

@keyframes lineFloat {
  0%, 100% {
    transform: translateX(0px) rotate(45deg);
    opacity: 0.6;
  }
  50% {
    transform: translateX(10px) rotate(50deg);
    opacity: 0.3;
  }
}

.banner {
  height: 300px;
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin-bottom: 40px;
  z-index: 2;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
  }
}

.banner-title {
  font-size: 3rem;
  color: white;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  position: relative;
  z-index: 3;
}

.home-container {
  display: flex;
  width: 100%;
  margin: 0;
  padding: 0 40px;
  animation: card_fadenum 0.8s;
  min-height: calc(100vh - 380px);
  position: relative;
  z-index: 2;
}

.tag-content {
  width: 100%;
}

.tag-card {
  background: var(--bg-card);
  border-radius: 20px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(227, 242, 253, 0.3);
  padding: 30px;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: var(--gradient-rainbow);
  }
}

// 标签标题区域
.tag-header {
  margin-bottom: 30px;
  text-align: center;
  padding-bottom: 20px;
  border-bottom: 2px solid rgba(227, 242, 253, 0.3);
}

.tag-title {
  font-size: 28px;
  font-weight: bold;
  color: var(--text-primary);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;

  i {
    color: var(--primary-color);
    font-size: 30px;
  }
}

.tag-description {
  font-size: 16px;
  color: var(--text-secondary);
  margin: 0 0 10px 0;
  font-style: italic;
}

.tag-stats {
  font-size: 14px;
  color: var(--primary-color);
  font-weight: 600;
  background: rgba(255, 105, 180, 0.1);
  padding: 6px 15px;
  border-radius: 20px;
  display: inline-block;
}

// 现代标签云样式
.tag-cloud-modern {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin: 20px 0 40px 0;
  padding: 20px;
  background: var(--bg-secondary);
  border-radius: 15px;
  border: 1px solid rgba(227, 242, 253, 0.3);
}

.tag-item {
  position: relative;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 25px;
  overflow: hidden;
  background: var(--bg-card);
  border: 2px solid transparent;

  &:hover, &.active {
    transform: translateY(-2px) scale(1.05);
    border-color: var(--primary-color);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);

    .tag-hover-effect {
      transform: scale(1);
      opacity: 1;
    }
  }

  &.active {
    border-color: var(--primary-color);
    background: rgba(255, 105, 180, 0.1);

    .tag-name {
      color: var(--primary-color);
      font-weight: 700;
    }
  }
}

.tag-content-wrapper {
  position: relative;
  z-index: 2;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.tag-name {
  font-weight: 600;
  color: var(--text-primary);
  transition: color 0.3s ease;
}

.tag-count {
  background: var(--gradient-accent);
  color: white;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
}

.tag-hover-effect {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--gradient-primary);
  opacity: 0;
  transform: scale(0.8);
  transition: all 0.3s ease;
  z-index: 1;
  border-radius: 25px;
}

// 不同大小的标签
.tag-size-xs {
  .tag-content-wrapper {
    padding: 6px 12px;
  }
  .tag-name {
    font-size: 12px;
  }
}

.tag-size-sm {
  .tag-content-wrapper {
    padding: 7px 14px;
  }
  .tag-name {
    font-size: 13px;
  }
}

.tag-size-md {
  .tag-content-wrapper {
    padding: 8px 16px;
  }
  .tag-name {
    font-size: 14px;
  }
}

.tag-size-lg {
  .tag-content-wrapper {
    padding: 10px 18px;
  }
  .tag-name {
    font-size: 16px;
    font-weight: 700;
  }
}

.tag-size-xl {
  .tag-content-wrapper {
    padding: 12px 20px;
  }
  .tag-name {
    font-size: 18px;
    font-weight: 700;
  }
  .tag-count {
    font-size: 13px;
    padding: 3px 10px;
  }
}

// 选中标签的文章列表
.selected-tag-articles {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 2px solid rgba(227, 242, 253, 0.3);
}

.selected-tag-header {
  margin-bottom: 20px;
  text-align: center;
}

.selected-tag-title {
  font-size: 20px;
  font-weight: bold;
  color: var(--text-primary);
  margin: 0 0 10px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;

  i {
    color: var(--primary-color);
    font-size: 22px;
  }
}

.selected-tag-info {
  font-size: 14px;
  color: var(--text-secondary);
  font-style: italic;
}

.articles-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 20px;
}

.article-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 20px;
  background: var(--bg-secondary);
  border-radius: 15px;
  border: 2px solid transparent;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 4px;
    height: 100%;
    background: var(--gradient-accent);
    transform: scaleY(0);
    transition: transform 0.3s ease;
  }

  &:hover {
    transform: translateY(-2px);
    border-color: var(--primary-color);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);

    &::before {
      transform: scaleY(1);
    }

    .article-arrow {
      transform: translateX(5px);
      opacity: 1;
    }

    .article-index {
      .index-number {
        background: var(--gradient-primary);
        color: white;
        transform: scale(1.1);
      }
    }

    .cover-overlay {
      opacity: 1;
    }
  }
}

// 文章序号区域
.article-index {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.index-number {
  width: 35px;
  height: 35px;
  border-radius: 50%;
  background: var(--bg-muted);
  color: var(--text-primary);
  font-weight: bold;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.article-type-icon {
  color: var(--primary-color);
  font-size: 16px;
  opacity: 0.7;
}

.article-cover {
  width: 120px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
  position: relative;

  .el-image {
    width: 100%;
    height: 100%;
  }
}

.cover-overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 8px;
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.3s ease;
  font-size: 14px;
}

.article-cover-placeholder {
  width: 120px;
  height: 80px;
  border-radius: 8px;
  background: var(--bg-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  font-size: 28px;
  flex-shrink: 0;
  border: 2px dashed rgba(227, 242, 253, 0.5);
}

.article-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.article-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.article-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  transition: color 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  i {
    color: var(--primary-color);
    font-size: 14px;
    flex-shrink: 0;
  }
}

.article-item:hover .article-title {
  color: var(--primary-color);
}

.article-badges {
  display: flex;
  gap: 5px;
  flex-shrink: 0;
}

.badge-top {
  background: var(--gradient-accent);
  color: white;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 3px;

  i {
    font-size: 10px;
  }
}

.article-summary {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  display: flex;
  align-items: flex-start;
  gap: 6px;

  i {
    color: var(--primary-color);
    font-size: 12px;
    margin-top: 2px;
    flex-shrink: 0;
    opacity: 0.7;
  }
}

.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  font-size: 13px;
  color: var(--text-secondary);
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 500;

  .el-icon {
    font-size: 14px;
  }

  span {
    font-weight: 600;
  }
}

.article-arrow {
  color: var(--primary-color);
  font-size: 20px;
  opacity: 0.6;
  transition: all 0.3s ease;
  flex-shrink: 0;
  align-self: center;
}

.article-pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 20px !important;
      margin: 0 6px !important;
      font-size: 16px !important;
      font-weight: 600 !important;
      background: white !important;
      border: 2px solid #e3f2fd !important;
      color: #666 !important;
      min-width: 40px !important;
      height: 40px !important;

      &:hover {
        border-color: #90caf9 !important;
        color: #1976d2 !important;
      }

      &.is-active {
        background: var(--bg-hover) !important;
        border-color: #1976d2 !important;
        color: #1976d2 !important;
      }
    }

    .btn-prev, .btn-next {
      border-radius: 20px !important;
      margin: 0 6px !important;
      font-size: 16px !important;
      font-weight: 600 !important;
      background: white !important;
      border: 2px solid #e3f2fd !important;
      color: #666 !important;
      min-width: 40px !important;
      height: 40px !important;

      &:hover:not(:disabled) {
        border-color: #90caf9 !important;
        color: #1976d2 !important;
      }

      &:disabled {
        opacity: 0.4 !important;
      }
    }
  }
}

.right-sidebar {
  flex: 6;
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-left: 40px;

  .el-card {
    border-radius: 15px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
}

@keyframes card_fadenum {
  0% {
    opacity: 0;
    transform: translateY(20px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

// 响应式设计
@media (max-width: 992px) {
  .home-container {
    padding: 0 20px;
  }

  .banner-title {
    font-size: 2rem;
  }

  .right-sidebar {
    margin-left: 0;
    margin-top: 20px;
  }

  .tag-cloud-modern {
    gap: 8px;
    padding: 15px;
  }
}

@media (max-width: 768px) {
  .banner {
    height: 200px;
  }

  .banner-title {
    font-size: 1.5rem;
  }

  .home-container {
    padding: 0 15px;
  }

  .tag-card {
    padding: 20px;
  }

  .tag-title {
    font-size: 22px;
    flex-direction: column;
    gap: 5px;
  }

  .tag-cloud-modern {
    gap: 6px;
    padding: 12px;
  }

  .article-item {
    padding: 15px;
    gap: 12px;
  }

  .article-cover, .article-cover-placeholder {
    width: 100px;
    height: 70px;
  }

  .article-title {
    font-size: 15px;
  }

  .index-number {
    width: 30px;
    height: 30px;
    font-size: 12px;
  }

  .article-arrow {
    font-size: 18px;
  }
}

@media (max-width: 480px) {
  .back-html {
    margin: 0 5px 5px 5px;
  }

  .tag-cloud-modern {
    justify-content: flex-start;
  }

  .article-item {
    flex-direction: column;
    text-align: center;
    gap: 10px;
    padding: 15px;
  }

  .article-index {
    flex-direction: row;
    justify-content: center;
    gap: 10px;
  }

  .article-cover, .article-cover-placeholder {
    width: 100%;
    height: 120px;
    align-self: center;
  }

  .article-header {
    flex-direction: column;
    align-items: center;
    gap: 5px;
  }

  .article-meta {
    justify-content: center;
    gap: 10px;
  }

  .article-arrow {
    align-self: center;
    font-size: 16px;
  }

  .articles-list {
    gap: 10px;
  }
}
</style>
